import request from '@/utils/request'

/**
 * 获取Dashboard统计数据
 */
export function getDashboardStats() {
  return request({
    url: '/api/dashboard/stats',
    method: 'get'
  })
}

/**
 * 获取最近登录记录
 */
export function getRecentLogins() {
  return request({
    url: '/api/dashboard/recent-logins',
    method: 'get'
  })
}

/**
 * 获取系统信息
 */
export function getSystemInfo() {
  return request({
    url: '/api/dashboard/system-info',
    method: 'get'
  })
}

// ==================== 模拟数据版本 ====================
// 在实际开发中这些应该来自后端API

export function getDashboardStatsMock() {
  return new Promise((resolve) => {
    setTimeout(() => {
      // 生成一些动态的模拟数据
      const now = new Date()
      const randomUserCount = Math.floor(Math.random() * 500) + 1000
      const randomRoleCount = Math.floor(Math.random() * 10) + 8
      const randomMenuCount = Math.floor(Math.random() * 20) + 30
      const randomVisits = Math.floor(Math.random() * 5000) + 5000
      
      resolve({
        code: 200,
        data: {
          userCount: randomUserCount,
          userCountChange: +(Math.random() * 10 - 2).toFixed(1), // -2% 到 +8%
          roleCount: randomRoleCount,
          roleCountChange: +(Math.random() * 4 - 1).toFixed(1), // -1% 到 +3%
          menuCount: randomMenuCount,
          menuCountChange: +(Math.random() * 6 - 1).toFixed(1), // -1% 到 +5%
          systemVisits: randomVisits,
          systemVisitsChange: +(Math.random() * 8 - 3).toFixed(1) // -3% 到 +5%
        },
        message: 'success',
        timestamp: now.getTime()
      })
    }, 300)
  })
}

export function getRecentLoginsMock() {
  return new Promise((resolve) => {
    setTimeout(() => {
      const now = new Date()
      const generateRandomIP = () => 
        `192.168.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}`
      
      const users = ['admin', 'user1', 'user2', 'user3', 'manager', 'guest']
      const statuses = ['成功', '成功', '成功', '成功', '失败'] // 80%成功率
      
      const logs = Array.from({ length: 5 }, (_, index) => {
        const time = new Date(now.getTime() - (index + 1) * 60 * 60 * 1000) // 每隔1小时
        return {
          username: users[Math.floor(Math.random() * users.length)],
          ip: generateRandomIP(),
          time: time.toLocaleString('zh-CN', { 
            year: 'numeric', 
            month: '2-digit', 
            day: '2-digit', 
            hour: '2-digit', 
            minute: '2-digit', 
            second: '2-digit' 
          }),
          status: statuses[Math.floor(Math.random() * statuses.length)]
        }
      })
      
      resolve({
        code: 200,
        data: logs,
        message: 'success'
      })
    }, 200)
  })
}